package com.leejxx.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leejxx.health.dao.CheckGroupDao;
import com.leejxx.health.dao.CheckMealDao;
import com.leejxx.health.entity.PageResult;
import com.leejxx.health.entity.QueryPageBean;
import com.leejxx.health.exception.BusinessRuntimeException;
import com.leejxx.health.pojo.CheckGroup;
import com.leejxx.health.pojo.CheckItem;
import com.leejxx.health.pojo.Setmeal;
import com.leejxx.health.service.CheckMealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author LeeJx
 * @ClassName CheckMealServiceImpl
 * @Description TODO
 * @date 2021/1/2 9:23
 * @Version 1.0
 */
@DubboService
@Slf4j
public class CheckMealServiceImpl implements CheckMealService {
    @Autowired
    private CheckMealDao checkMealDao;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Setmeal> page = checkMealDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<CheckGroup> findAllCheckGroups() {
        return checkMealDao.findAllCheckGroups();
    }

    @Override
    @Transactional
    public void add(Setmeal checkMeal, List<Integer> checkGroupIds) {
        List<String> codes = checkMealDao.findAllCodes();
        for (String code : codes) {
            if (code.equals(checkMeal.getCode())) {
                throw new BusinessRuntimeException("当前编码已被使用，请重新输入");
            }
        }
        if (null == checkGroupIds) {
            throw new RuntimeException("检查组信息为空，请选择");
        }
        checkMealDao.add(checkMeal);
        checkMealDao.addCheckGroupIds(checkMeal.getCode(), checkGroupIds);
    }

    @Override
    public long countByImg(String img) {
        return checkMealDao.countByImg(img);
    }

    @Override
    public Setmeal findCheckMealById(Integer id) {
        Setmeal setmeal = checkMealDao.findCheckMealById(id);

        //查询对应的检查组
//        List<CheckGroup> checkGroups = checkMealDao.findCheckGroupByMealId(id);
//        setmeal.setCheckGroups(checkGroups);

        return setmeal;
    }

    @Override
    public List<Integer> findCheckGroupIdByMealId(Integer id) {
        return checkMealDao.findCheckGroupIdByMealId(id);
    }

    @Override
    public void editCheckMeal(Setmeal checkMeal) {
        checkMealDao.editCheckMeal(checkMeal);
    }

    @Override
    @Transactional
    public void editCheckGroupId(Integer id, List<Integer> checkGroupIds) {
        //先删后增
        checkMealDao.deleteId(id);
        checkMealDao.insert(id, checkGroupIds);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        //先删关系后删套餐信息
        checkMealDao.deleteId(id);
        checkMealDao.delete(id);
    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal checkMeal = checkMealDao.finById(id);
        List<Integer> groupIds = checkMealDao.findCheckGroupIdByMealId(id);
        List<CheckGroup> checkGroups = new ArrayList<>();
        for (Integer groupId : groupIds) {
            CheckGroup checkGroup = checkGroupDao.findById(groupId);
            checkGroups.add(checkGroup);
        }

        List<CheckItem> checkItems = new ArrayList<>();
        for (CheckGroup checkGroup : checkGroups) {
            checkItems = checkGroupDao.findCheckItemsOfCheckGroup(checkGroup.getId());
            checkGroup.setCheckItems(checkItems);
        }

        checkMeal.setCheckGroups(checkGroups);
        
        return checkMeal;
    }

    @Override
    public Setmeal findByMealId(Integer checkMealId) {
        return checkMealDao.findByMealId(checkMealId);
    }

    @Override
    public List<Setmeal> findAllCheckMeal() {
        return checkMealDao.findAllCheckMeal();
    }
}
